/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


************************************************************************************************/
using DotNetNuke;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.Enums;
using Votations.NSurvey.WebControls.UI;
using Votations.NSurvey.WebAdmin.UserControls;

namespace Votations.NSurvey.WebAdmin
{
	public class ResultsReporting : PageBase
	{
		protected SurveyListControl SurveyList;
		protected System.Web.UI.WebControls.Label MessageLabel;
		protected System.Web.UI.WebControls.PlaceHolder ResultsPlaceHolder;
		protected System.Web.UI.WebControls.Repeater ChartRepeater;
		protected System.Web.UI.WebControls.DropDownList QuestionsDropDownList;
		protected System.Web.UI.WebControls.DropDownList LayoutDropDownList;
		protected System.Web.UI.WebControls.Image SingleChartImage;
		protected System.Web.UI.WebControls.DropDownList FilterDropDownList;
		protected System.Web.UI.WebControls.Literal SurveyResultsTitle;
		protected System.Web.UI.WebControls.Literal QuestionsResultsDisplaylabel;
		protected System.Web.UI.WebControls.Literal ResultsLayoutLabel;
		protected System.Web.UI.WebControls.Literal ApplyFilter;
		protected System.Web.UI.WebControls.Literal ApplyFilterLabel;
		protected System.Web.UI.WebControls.HyperLink FilterEditorHyperLink;
		protected System.Web.UI.WebControls.Literal ResultsOrderLabel;
		protected System.Web.UI.WebControls.DropDownList ResultsOrderDropDownList;

		protected HeaderControl Header;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			
			CreateLinks();
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.QuestionsDropDownList.SelectedIndexChanged += new System.EventHandler(this.QuestionsDropDownList_SelectedIndexChanged);
			this.LayoutDropDownList.SelectedIndexChanged += new System.EventHandler(this.QuestionsDropDownList_SelectedIndexChanged);
			this.ResultsOrderDropDownList.SelectedIndexChanged += new System.EventHandler(this.QuestionsDropDownList_SelectedIndexChanged);
			this.FilterDropDownList.SelectedIndexChanged += new System.EventHandler(this.FilterDropDownList_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				SetupSecurity();
				LocalizePage();

				if (!Page.IsPostBack)
				{
					SurveyList.SurveyId = SurveyId;
					SurveyList.BindDropDown();
					Header.SurveyId = SurveyId;
					QuestionsDropDownList.DataSource = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
					QuestionsDropDownList.DataTextField = "QuestionText";
					QuestionsDropDownList.DataValueField = "QuestionID";
					QuestionsDropDownList.DataBind();
					QuestionsDropDownList.Items.Insert(0, new ListItem(GetPageResource("DisplayAllResultsMessage"), "0"));
					QuestionsDropDownList.Items.Insert(0, new ListItem(GetPageResource("SelectQuestionMessage"), "-1"));
		
					FilterDropDownList.DataSource = new Filters().GetFilters(SurveyId);
					FilterDropDownList.DataMember = "Filters";
					FilterDropDownList.DataTextField = "Description";
					FilterDropDownList.DataValueField = "FilterID";
					FilterDropDownList.DataBind();
					FilterDropDownList.Items.Insert(0, 
						new ListItem(GetPageResource("SelectFilterMessage"),"-1"));

					LayoutDropDownList.Items.Add(new ListItem(GetPageResource("ColumnChartOption"),"1"));
					LayoutDropDownList.Items.Add(new ListItem(GetPageResource("HTMLChartOption"),"2"));
					FilterEditorHyperLink.Text = GetPageResource("FilterEditorHyperLink");
					FilterEditorHyperLink.NavigateUrl = UINavigator.FilterEditor + "&SurveyId=" + SurveyId + "&menuindex=" + MenuIndex;
				}

			}
			catch( Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this,exc);
			}
		}

		private void SetupSecurity()
		{
			CheckRight(NSurveyRights.AccessReports, true);
			FilterEditorHyperLink.Visible = CheckRight(NSurveyRights.CreateResultsFilter, false);
		}

		private void LocalizePage()
		{

			SurveyResultsTitle.Text = GetPageResource("SurveyResultsTitle");
			QuestionsResultsDisplaylabel.Text = GetPageResource("QuestionsResultsDisplaylabel");
			ResultsLayoutLabel.Text = GetPageResource("ResultsLayoutLabel");
			ApplyFilterLabel.Text = GetPageResource("ApplyFilterLabel");
			ResultsOrderLabel.Text = GetPageResource("ResultsOrderLabel");
			if (!Page.IsPostBack)
			{
				ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("AnswerOrderOption"),"ANS"));
				ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("AscendingOrderOption"),"ASC"));
				ResultsOrderDropDownList.Items.Add(new ListItem(GetPageResource("DescendingOrderOption"),"DESC"));
			}
		}

		/// <summary>
		/// Get the current DB stats and build 
		/// the results tables
		/// </summary>
		private void BuildHtmlResults()
		{
			QuestionData questionsData = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
			ChartRepeater.Visible = false;
			SingleChartImage.Visible = false;
			
			Table resultsTable = new Table();
			resultsTable.CellSpacing = 10;
			TableCell resultsTableCell = new TableCell();
			TableRow resultsTableRow = new TableRow();

			resultsTable.CssClass = "resultsTable";
			foreach (QuestionData.QuestionsRow question in questionsData.Questions.Rows)
			{
				resultsTableCell = new TableCell();
				resultsTableRow = new TableRow();
				Style questionStyle = new Style();
				Style answerStyle = new Style();
				questionStyle.Font.Bold = true;
				questionStyle.Font.Size = FontUnit.XSmall;
				questionStyle.ForeColor = System.Drawing.Color.Black;
				//questionStyle.BackColor = System.Drawing.Color.FromArgb(225,238,241);
				answerStyle.Font.Size = FontUnit.XXSmall;
				ResultItem questionResults = new ResultItem();
				questionResults.BorderWidth = Unit.Pixel(1);
				questionResults.BorderColor = System.Drawing.Color.FromArgb(123,123,123);
				questionResults.BackColor = System.Drawing.Color.FromArgb(243,243,243);
				questionResults.Width = Unit.Pixel(600);
				questionResults.BarColor = "blueadmin";
				questionResults.QuestionStyle = questionStyle;
				questionResults.AnswerStyle = answerStyle;
				questionResults.FootStyle = answerStyle;
				questionResults.DataSource =new Questions().GetQuestionResults(question.QuestionId, int.Parse(FilterDropDownList.SelectedValue), ResultsOrderDropDownList.SelectedValue);
				questionResults.DataBind();
				resultsTableCell.Controls.Add(questionResults);
					
				resultsTableRow.Cells.Add(resultsTableCell);
				resultsTable.Rows.Add(resultsTableRow);
			}

			ResultsPlaceHolder.Controls.Add(resultsTable);
		}

		private void QuestionsDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (QuestionsDropDownList.SelectedValue != "-1")
			{
				if (LayoutDropDownList.SelectedValue == "2")
				{
					BuildHtmlResults();
				}
				else
				{
					BuildChartColumnResults(int.Parse(QuestionsDropDownList.SelectedValue));
				}
			}
		
		}

		void BuildChartColumnResults(int questionId)
		{
			if (questionId == 0)
			{
				ChartRepeater.Visible = true;
				SingleChartImage.Visible = false;
				ChartRepeater.DataSource = new Questions().GetQuestionListWithSelectableAnswers(SurveyId);
				ChartRepeater.DataBind();
			}
			else
			{
				ChartRepeater.Visible = false;
				SingleChartImage.Visible = true;
				SingleChartImage.ImageUrl = Server.HtmlDecode("BarChartReport.aspx?questionid="+questionId+"&filterid="+FilterDropDownList.SelectedValue+"&SortOrder="+ResultsOrderDropDownList.SelectedValue);
			}
		}

		private void FilterDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (QuestionsDropDownList.SelectedValue != "-1")
			{
				if (LayoutDropDownList.SelectedValue == "2")
				{
					BuildHtmlResults();
				}
				else
				{
					BuildChartColumnResults(int.Parse(QuestionsDropDownList.SelectedValue));
				}
			}		
		}

	}
}
